import { ENUM_WATER_TYPE } from "../Enum";
import { DATA_GAME } from "../manager/DataManager";


const { ccclass, property } = cc._decorator;

export class WaterInfo {
    public color: number
    public left_down: cc.Vec2
    public left_up: cc.Vec2
    public right_down: cc.Vec2
    public right_up: cc.Vec2
    public up: cc.Vec2
    public down: cc.Vec2
}

@ccclass
export default class Water extends cc.Component {

    graphics: cc.Graphics = null
    info: WaterInfo = null

    init(params: WaterInfo) {
        this.graphics = this.node.getChildByName('graphics').getComponent(cc.Graphics)
        this.info = params
        this.graphics.clear();
        this.graphics.fillColor = new cc.Color().fromHEX(DATA_GAME.colors[params.color])
        this.graphics.moveTo(params.left_down.x, params.left_down.y);
        this.graphics.lineTo(params.right_down.x, params.right_down.y);
        this.graphics.lineTo(params.right_up.x, params.right_up.y);
        this.graphics.lineTo(params.left_up.x, params.left_up.y);
        this.graphics.fill();
    }

    reset(params: WaterInfo) {
        this.graphics.clear();
        this.graphics.fillColor = new cc.Color().fromHEX(DATA_GAME.colors[params.color])
        this.graphics.moveTo(params.left_down.x, params.left_down.y);
        this.graphics.lineTo(params.right_down.x, params.right_down.y);
        this.graphics.lineTo(params.right_up.x, params.right_up.y);
        this.graphics.lineTo(params.left_up.x, params.left_up.y);
        this.graphics.fill();
    }

    setPos(up: cc.Vec2, down: cc.Vec2) {
        this.info.up = up
        this.info.down = down
    }

    getPos(type: ENUM_WATER_TYPE, isWorld: boolean = false) {
        let pos = null
        switch (type) {
            case ENUM_WATER_TYPE.UP:
                pos = isWorld ? this.node.convertToWorldSpaceAR(this.info.up) : this.info.up
                break
            case ENUM_WATER_TYPE.DOWN:
                pos = isWorld ? this.node.convertToWorldSpaceAR(this.info.down) : this.info.down
                break
            case ENUM_WATER_TYPE.LEFT_UP:
                pos = isWorld ? this.node.convertToWorldSpaceAR(this.info.left_up) : this.info.left_up
                break
            case ENUM_WATER_TYPE.LEFT_DOWN:
                pos = isWorld ? this.node.convertToWorldSpaceAR(this.info.left_down) : this.info.left_down
                break
            case ENUM_WATER_TYPE.RIGHT_UP:
                pos = isWorld ? this.node.convertToWorldSpaceAR(this.info.right_up) : this.info.right_up
                break
            case ENUM_WATER_TYPE.RIGHT_DOWN:
                pos = isWorld ? this.node.convertToWorldSpaceAR(this.info.right_down) : this.info.right_down
                break
        }
        return pos
    }

}
